package _1_初级._2_字符串.反转字符串;

import com.alibaba.fastjson.JSON;

public class _917_仅仅反转字母 {

    /**
     * 给定一个字符串 S，返回 “反转后的” 字符串，其中不是字母的字符都保留在原地，而所有字母的位置发生反转。
     * 
     *
     * 示例 1：
     *
     * 输入："ab-cd"
     * 输出："dc-ba"
     * 示例 2：
     *
     * 输入："a-bC-dEf-ghIj"
     * 输出："j-Ih-gfE-dCba"
     * 示例 3：
     *
     * 输入："Test1ng-Leet=code-Q!"
     * 输出："Qedo1ct-eeLg=ntse-T!"
     *  
     *
     * 提示：
     *
     * S.length <= 100
     * 33 <= S[i].ASCIIcode <= 122 
     * S 中不包含 \ or "
     *
     * @param S
     * @return
     */
    public static String reverseOnlyLetters(String S) {

        char[] charArray = S.toCharArray();
        int leftCode;
        int rightCode;
        char temp;

        for(int i = 0,j = charArray.length -1; i < j; i++,j--){
            leftCode = charArray[i];
            rightCode = charArray[j];
            while(
                    !((leftCode >= 65 && leftCode <= 90) || (leftCode >= 97 && leftCode <= 122))
            )
            {
                i++;
                leftCode = charArray[i];
            }
            while (
                    !((rightCode >= 65 && rightCode <= 90) || (rightCode >= 97 && rightCode <= 122))
                    )
            {
                j--;
                rightCode = charArray[j];
            }
            temp = charArray[j];
            charArray[j] = charArray[i];
            charArray[i] = temp;
        }

        return new String(charArray);
    }

    public static void main(String[] args) {
        String s = "ab-cd";
        String s1 = "a-bC-dEf-ghIj";
        String s2 = "Test1ng-Leet=code-Q!";
        System.out.println(JSON.toJSONString(reverseOnlyLetters(s2)));
    }
}
